
import store from '../store'
import Vue from 'vue'
export const BASE_URL = 'https://wjlm.goldsu.cn/'
//商城中 普通网络请求 
export const myRequest = (url, data, method) => {
	//加载动画
	store.commit('switch', {
		code: true
	})
	var token = uni.getStorageSync("token") || "";

	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			method: method || 'GET',
			data: data || {},
			header: {
				"XX-Device-Type": "wxapp",
				'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
				"XX-User-Type": 'repo',
				"XX-Token": token,
			},
			success: (res) => {
				store.commit('switch', {
					code: false
				})
				if (res.data.code == '501') {//登录状态失效
				 //清除个人信息
					uni.removeStorageSync('userInfo');
					uni.removeStorageSync('token');
					uni.showToast({
						title: '登录已失效',
						icon: "none"
					})
					setTimeout(() => {

						let pages = getCurrentPages()
						let curPage = pages[pages.length - 1]
						// let fullPath = ''

						if (curPage.route !== 'pages/login/login') {
							uni.navigateTo({
								url: "/pages/login/login",
								complete(err) {
									console.log('错误', err)
								}
							})
						}
					}, 1000)

				} else if (res.data.code == 200) {
					resolve(res.data)
				} else {
					let msg=res.data.code == 502?'登录状态已失效，请重试！':'未知问题，请刷新重试！'
					uni.showToast({
						title: res.data.msg||msg,
						icon: "none"
					})
					reject(res.data)
				}
			},
			fail: (err) => {
				store.commit('switch', {
					code: false
				})
				uni.showToast({
					title: '请求接口失败',
					icon: "none"
				})
				reject(err.data)
			}
		})
	}).catch((e) => {})
}


// 活动中 特殊网络请求  *待完善（与普通请求整合 优化代码）
export const myRequestWs = (url, data, method) => {
	store.commit('switch', {
		code: true
	})
	var token = uni.getStorageSync("token") || "";

	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			method: method || 'GET',
			data: data || {},
			header: {
				"XX-Device-Type": "wxapp",
				'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
				"XX-User-Type": 'repo',
				"XX-Token": token,
			},
			success: (res) => {
				store.commit('switch', {
					code: false
				})

				if (res.data.code == '501') {
					uni.removeStorageSync('userInfo');
					uni.removeStorageSync('token');
					uni.showToast({
						title: '登录已失效',
						icon: "none"
					})
					setTimeout(() => {
						let pages = getCurrentPages()
						let curPage = pages[pages.length - 1]
						// let fullPath = ''

						if (curPage.route !== 'pages/login/login') {
							// 					fullPath = curPage.$page.fullPath
							uni.navigateTo({
								url: "/pages/login/login",
								complete(err) {
									console.log('what', err)
								}
							})
						}
					}, 1000)

				} else if (res.data.code == 200) {
					resolve(res.data)
					let game_id = uni.getStorageSync("gameId")
					let userData = uni.getStorageSync("userData")
				  Vue.prototype.$ws.send(JSON.stringify({game_id,user_id:userData.id}))
				} else {
					uni.showToast({
						title: res.data.msg||'未知错误，请重试！',
						icon: "none"
					})
				}
			},
			fail: (err) => {
				store.commit('switch', {
					code: false
				})
				uni.showToast({
					title: '请求接口失败',
					icon: "none"
				})
				reject(err.data)
			}
		})
	}).catch((e) => {})
}



			
